package tree;

public class ArrayBinaryTree {
    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
        ABTree tree = new ABTree(arr);
        tree.preOrder();
    }
}

class ABTree {
    private int arr[];
    public ABTree(int arr[]) {
        this.arr = arr;
    }

    public void preOrder() {
        System.out.println("先序遍历");
        if (arr != null && arr.length > 0) {
            preOrder(0);
        } else {
            System.out.println("树为空，无法遍历");
        }
    }

    private void preOrder(int index) {
        System.out.println(arr[index]);
        if (index * 2 + 1 < arr.length) {
            preOrder(index * 2 + 1);
        }
        if (index * 2 + 2 < arr.length) {
            preOrder(index * 2 + 2);
        }
    }
}
